﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace HirelSeriesH.Entity
{
    public class Position
    {
        /// <summary>
        /// 十点平滑
        /// </summary>
        private List<decimal> agvBuffer = new List<decimal>();

        private decimal preValue = -1;
        /// <summary>
        /// 连续五次过流值
        /// </summary>
        private List<decimal> excBuffer = new List<decimal>();

        /// <summary>
        /// 连续五次短路值
        /// </summary>
        private List<decimal> shortBuffer = new List<decimal>();

        private int posid;

        private Channel channel;

        private bool isOver = false;

        private bool isShort = false;

        private bool isFuse = false;
        /// <summary>
        /// 短路保护计时器
        /// </summary>
        private Timer timerForDirectShort;

        private int shortCount = 0;

        /// <summary>
        /// 短路保护计时器
        /// </summary>
        private Timer timerForShort;

        /// <summary>
        /// 基础值缓冲区
        /// </summary>
        private List<decimal> buffer64 = new List<decimal>();

        private List<decimal> overDataBuffer = new List<decimal>();

        private decimal iValue = 0;

        private decimal uValue = 0;

        private string iD;

        private string chID;

        private string channelName;

        private int index;

        private decimal basicValue = 0;

        private bool basicFlag = false;

        /// <summary>
        /// 工位校正K系数
        /// </summary>
        private decimal gWJZ_K;
        /// <summary>
        /// 工位校正B常数
        /// </summary>
        private decimal gWJZ_B;
        /// <summary>
        /// 工位校正×1K系数
        /// </summary>
        private decimal gWJZ1_K;
        /// <summary>
        /// 工位校正×1B常数
        /// </summary>
        private decimal gWJZ1_B;
        /// <summary>
        /// 工位校正×10K系数
        /// </summary>
        private decimal gWJZ10_K;
        /// <summary>
        /// 工位校正×10B常数
        /// </summary>
        private decimal gWJZ10_B;
        /// <summary>
        /// 工位校正×100K系数
        /// </summary>
        private decimal gWJZ100_K;
        /// <summary>
        /// 工位校正×100B常数
        /// </summary>
        private decimal gWJZ100_B;
        /// <summary>
        /// 工位校正×1000K系数
        /// </summary>
        private decimal gWJZ1000_K;
        /// <summary>
        /// 工位校正×1000B常数
        /// </summary>
        private decimal gWJZ1000_B;

        private bool isUse = true;

        public Position(Channel ch, int id)
        {
            Index = id;
            posid = id;
            channel = ch;
            TimerForDirectShort = new Timer();
            TimerForDirectShort.Interval = 4000;
            TimerForDirectShort.AutoReset = true;
            TimerForDirectShort.Enabled = false;
            TimerForDirectShort.Elapsed += new ElapsedEventHandler(TimerForDirectShort_Tick);

            TimerForShort = new Timer();
            TimerForShort.Interval = 3000;
            TimerForShort.AutoReset = true;
            TimerForShort.Enabled = false;
            TimerForShort.Elapsed += new ElapsedEventHandler(TimerForShort_Tick);
        }

        public List<decimal> AgvBuffer { get => agvBuffer; set => agvBuffer = value; }
        public decimal PreValue { get => preValue; set => preValue = value; }
        public List<decimal> ExcBuffer { get => excBuffer; set => excBuffer = value; }
        public bool IsShort { get => isShort; set => isShort = value; }
        public bool IsFuse { get => isFuse; set => isFuse = value; }
        public List<decimal> ShortBuffer { get => shortBuffer; set => shortBuffer = value; }
        public Timer TimerForDirectShort { get => timerForDirectShort; set => timerForDirectShort = value; }
        public List<decimal> Buffer64 { get => buffer64; set => buffer64 = value; }
        public decimal BasicValue { get => basicValue; set => basicValue = value; }
        public bool BasicFlag { get => basicFlag; set => basicFlag = value; }
        public bool IsOver { get => isOver; set => isOver = value; }
        public string ID { get => iD; set => iD = value; }
        public string ChID { get => chID; set => chID = value; }
        public string ChannelName { get => channelName; set => channelName = value; }
        public int Index { get => index; set => index = value; }
        public int ShortCount { get => shortCount; set => shortCount = value; }
        public Timer TimerForShort { get => timerForShort; set => timerForShort = value; }
        public List<decimal> OverDataBuffer { get => overDataBuffer; set => overDataBuffer = value; }
        public decimal GWJZ_K { get => gWJZ_K; set => gWJZ_K = value; }
        public decimal GWJZ_B { get => gWJZ_B; set => gWJZ_B = value; }
        public decimal GWJZ1_K { get => gWJZ1_K; set => gWJZ1_K = value; }
        public decimal GWJZ1_B { get => gWJZ1_B; set => gWJZ1_B = value; }
        public decimal GWJZ10_K { get => gWJZ10_K; set => gWJZ10_K = value; }
        public decimal GWJZ10_B { get => gWJZ10_B; set => gWJZ10_B = value; }
        public decimal GWJZ100_K { get => gWJZ100_K; set => gWJZ100_K = value; }
        public decimal GWJZ100_B { get => gWJZ100_B; set => gWJZ100_B = value; }
        public decimal GWJZ1000_K { get => gWJZ1000_K; set => gWJZ1000_K = value; }
        public decimal GWJZ1000_B { get => gWJZ1000_B; set => gWJZ1000_B = value; }
        public decimal IValue { get => iValue; set => iValue = value; }
        public decimal UValue { get => uValue; set => uValue = value; }

        public bool IsUse
        {
            get
            {
                return isUse;
            }

            set
            {
                isUse = value;
            }
        }

        private void TimerForDirectShort_Tick(object sender, EventArgs e)
        {
            if (channel.State != "running")
            {
                TimerForDirectShort.Stop();
                return;
            }

            bool flag = false;

            foreach (var pos in channel.Positions)
            {
                if (pos.TimerForShort.Enabled == true)
                {
                    flag = true;
                    break;
                }
            }
            if (shortBuffer.Count != 0 && !flag)
            {
                TimerForShort.Stop();
                shortCount = 0;
                TimerForShort.Start();
            }
            TimerForDirectShort.Enabled = false;
            ShortBuffer.Clear();
        }
        //
        private void TimerForShort_Tick(object sender, EventArgs e)
        {
            decimal volt = channel.Power.Voltage;
            if (!channel.Power.IsOpen || volt <= 0)
            {
                TimerForShort.Stop();
                return;
            }
            decimal rvolt = channel.Power.RealVoltage;
            decimal value = Math.Abs(volt - rvolt) / volt;

            if (value > 0.05m && !channel.Power.IsUpDown)
            {
                shortCount++;
                if (shortCount == 20)
                {
                    shortCount = 0;
                    TimerForShort.Stop();
                    channel.directShort(channel.Index, posid);
                }
            }
            else
            {
                if (channel.Power.IsUpDown)
                {
                    TimerForShort.Stop();
                }
                shortCount = 0;
                TimerForShort.Stop();
            }
            Debug.WriteLine("目标电压：" + volt + " 实际电压：" + rvolt + " 差额比：" + value + "计时轮次：" + shortCount);
        }
    }
}
